﻿using System.Linq;
using System.Collections.Generic;
using QMSDAL;

namespace QuaManSys
{
    public class processStatus
    {
        /// <summary>
        /// 查询状态为处理的内部质量信息条数
        /// </summary>
        /// <returns></returns>
        public static List<InnerQualityInfo> QueryInnerStatusPro(CurUser user)
        {
            using (QuaManSysEntities context = new QuaManSysEntities())
            {
                if (Permission.JudgePermission(user, "1B", "D1"))
                {
                    var inInfoPro =
                    from c in context.InnerQualityInfo
                    where c.processingStatus == "处理"
                    select c;
                    List<InnerQualityInfo> list = inInfoPro.ToList();

                    foreach (var item in inInfoPro)
                    {
                        string InfoId = item.qualityInfoId;
                        var inFile =
                            from q in context.InnerResponsibleDepart
                            where InfoId == q.qualityInfoId
                            select q;
                        //记录可进行归档的数据记录条数
                        list.Add(item);
                        foreach (var itm in inFile)
                        {
                            if (itm.processed != 1)
                            {
                                list.Remove(item);
                                break;
                            }
                        }
                    }
                    //int fileNumer = inInfoPro.Count();
                    return list;
                }
                else if (Permission.JudgePermission(user, "1B", "B1"))
                {
                    var inInfoPro =
                    from c in context.InnerQualityInfo
                    join o in context.InnerResponsibleDepart on c.qualityInfoId equals o.qualityInfoId
                    where c.processingStatus == "处理" && user.department == o.department
                        && o.processed == 0
                    select c;
                    List<InnerQualityInfo> list = inInfoPro.ToList();
                    return list;
                }
                return null;
            }
        }

        /// <summary>
        /// 查询"状态为处理"责任部门为department的内部质量信息条数
        /// </summary>
        /// <param name="repdepartment"></param>
        /// <returns></returns>
        public static List<InnerQualityInfo> QueryInnerResStatusPro(CurUser user)
        {
            using (QuaManSysEntities context = new QuaManSysEntities())
            {
                if (Permission.JudgePermission(user, "1B", "B1"))
                {
                    var inInfoResStatusPro =
                    from c in context.InnerQualityInfo
                    join o in context.InnerResponsibleDepart on c.qualityInfoId equals o.qualityInfoId
                    where c.processingStatus == "处理" && user.department == o.department
                        && o.isResponDepart == 1 && o.processed == 0
                    select c;
                    List<InnerQualityInfo> list = inInfoResStatusPro.ToList();
                    return list;
                }
                return null;
            }
        }

        /// <summary>
        /// 查询状态为处理的外部质量信息条数
        /// </summary>
        /// <returns></returns>
        public static List<OuterQualityInfo> QueryOuterStatusPro(CurUser user)
        {
            using (QuaManSysEntities context = new QuaManSysEntities())
            {
                if (Permission.JudgePermission(user, "2B", "D0"))
                {
                    var outInfoPro =
                    from c in context.OuterQualityInfo
                    where c.processingStatus == "处理"
                    select c;
                    List<OuterQualityInfo> list = outInfoPro.ToList();
                    return list;
                }
                else if (Permission.JudgePermission(user, "2B", "B1"))
                {
                    var outInfoPro =
                    from c in context.OuterQualityInfo
                    join o in context.OuterResponsibleDepart on c.qualityInfoId equals o.qualityInfoId
                    where c.processingStatus == "处理" && user.department == o.department
                    select c;
                    List<OuterQualityInfo> list = outInfoPro.ToList();
                    return list;
                }
                else
                    return null;
            }
        }

        /// <summary>
        /// 查询"状态为处理"责任部门为department的内部质量信息条数
        /// </summary>
        /// <param name="repdepartment"></param>
        /// <returns></returns>
        public static List<OuterQualityInfo> QueryOuterResStatusPro(CurUser user)
        {
            using (QuaManSysEntities context = new QuaManSysEntities())
            {
                if (Permission.JudgePermission(user, "2B", "B1"))
                {
                    var outInfoResStatusPro =
                    from c in context.OuterQualityInfo
                    join o in context.OuterResponsibleDepart on c.qualityInfoId equals o.qualityInfoId
                    where c.processingStatus == "处理" && user.department == o.department
                        && o.isResponDepart == 1
                    select c;
                    List<OuterQualityInfo> list = outInfoResStatusPro.ToList();
                    return list;
                }
                return null;
            }
        }
    }
}